#include "DisplayWindow.h"
#include <QtGui\qapplication.h>

using namespace std;
using namespace GUI;

DisplayWindow::DisplayWindow()
	:mLeft(0),
	 mTop(0),
	 mWidth(0),
	 mHeight(0),
	 mpForm(NULL)
{
	mTitle = qApp->applicationName().toLocal8Bit().constData();
	Restore();
	UpdateContext();
}

DisplayWindow::~DisplayWindow()
{
	Clear();
}

DisplayWindow& DisplayWindow::SetTitle(const string& inTitle)
{
	mTitle = inTitle;
	if(mpForm!= NULL)
	{
		mpForm->setWindowTitle(QString::fromLocal8Bit(mTitle.c_str()));
	}
	return *this;
}

const string& DisplayWindow::Title() const
{
	return mTitle;
}

DisplayWindow& DisplayWindow::SetLeft(int inLeft)
{
	mLeft = inLeft;
	if(mpForm != NULL)
	{
		mpForm->move(mLeft, mTop);
		UpdateContext();
	}
	return *this;
}

int DisplayWindow::Left() const
{ return mLeft; }

DisplayWindow& DisplayWindow::SetTop(int inTop)
{
	mTop = inTop;
	if(mpForm != NULL)
	{
		mpForm->move(mLeft, mTop);
		UpdateContext();
	}
	return *this;
}

int DisplayWindow::Top() const
{ return mTop; }

DisplayWindow& DisplayWindow::SetWidth(int inWidth)
{
	mWidth = inWidth;
	if(mpForm != NULL)
	{
		mpForm->resize(mWidth, mHeight);
		UpdateContext();
	}

	return *this;
}

int DisplayWindow::Width() const
{ return mWidth; }

DisplayWindow& DisplayWindow::SetHeight(int inHeight)
{
	mHeight = inHeight;
	if(mpForm != NULL)
	{
		mpForm->resize(mWidth, mHeight);
		UpdateContext();
	}
	return *this;
}


int DisplayWindow::Height() const
{ return mHeight; }

DisplayWindow& DisplayWindow::Show()
{
	if(mpForm == NULL)
		Restore();
	mpForm->show();
	UpdateContext();
	return *this;
}

DisplayWindow& DisplayWindow::Hide()
{
	mpForm->hide();
	return *this;
}

bool DisplayWindow::Visible() const
{
	return mpForm ? mpForm->isVisible():false;
}

DisplayWindow& DisplayWindow::Restore()
{
	mpForm = new QWidget;
	mpForm->setWindowFlags(Qt::FramelessWindowHint);
	mpForm->setAttribute(Qt::WA_NoSystemBackground);
	mpForm->move(mLeft, mTop);
	mpForm->resize(mWidth, mHeight);
	mpForm->setWindowTitle(QString::fromLocal8Bit(mTitle.c_str()));

	return *this;
}

DisplayWindow& DisplayWindow::Clear()
{
	delete mpForm;
	mpForm = NULL;
	UpdateContext();

	return *this;
}

DisplayWindow& DisplayWindow::UpdateContext()
{
	DrawContext dc = {
		{mpForm, NULL, NULL},
		{0, 0, mWidth, mHeight},
	};
	GraphDisplay::SetContext(dc);

	return *this;
}